﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Common;
using System.Data.SqlClient;

using JM.JCY.BusinessEntity;
using JM.JCY.Common;
using JM.JCY.Common.Utilities;
using System.Data;

namespace JM.JCY.DataAccess
{
   public class NewsfeedDA:JCYDataAccessBasecs
    {
       public NewsfeedDataSet.T_FeedPostDataTable GetFeedPost(string appType, int appId, string excludePostId)
       {
           NewsfeedDataSet ds = new NewsfeedDataSet();
           base.Helper.Fill(ds.T_FeedPost, "P_GetFeedPost", appType, appId, excludePostId);
           return ds.T_FeedPost;
       }

       public NewsfeedDataSet.T_FeedPostDataTable GetMoreFeedPost(string appType, int appId, string excludePostId)
       {
           NewsfeedDataSet ds = new NewsfeedDataSet();
           base.Helper.Fill(ds.T_FeedPost, "P_GetMoreFeedPost", appType, appId, excludePostId);
           return ds.T_FeedPost;
       }

       public void InsertFeedPost(NewsfeedDataSet ds)
       {
           string content = ds.T_FeedPost.Rows[0]["PostContent"].ToString();
           string _content = HtmlSanitizer.MakeHtmlSafe(content);
           ds.T_FeedPost.Rows[0]["PostContent"] = _content;
           Helper.Update(ds.T_FeedPost);
       }

       public NewsfeedDataSet.T_FeedPostDataTable GetFeedPostEntity(int feedPostId)
       {
           NewsfeedDataSet ds = new NewsfeedDataSet();
           base.Helper.Fill(ds.T_FeedPost, "P_FeedPost_S_ID", feedPostId);
           return ds.T_FeedPost;
       }

       public NewsfeedDataSet.T_FeedPostDataTable GetFeedPostByObjectId(int objId)
       {
           NewsfeedDataSet ds = new NewsfeedDataSet();
           base.Helper.Fill(ds.T_FeedPost, "P_FeedPost_S_ObjId", objId);
           return ds.T_FeedPost;
       }

       public NewsfeedDataSet.T_FeedCommentDataTable InsertComment(NewsfeedDataSet ds)
       {
           string comment = ds.T_FeedComment.Rows[0]["Content"].ToString();
           string _comment = HtmlSanitizer.MakeHtmlSafe(comment);
           ds.T_FeedComment.Rows[0]["Content"] = _comment;
           Helper.Update(ds.T_FeedComment);
           return GetFeedCommentEntity(ds.T_FeedComment[0].FeedCommentId);
       }

       public NewsfeedDataSet.T_FeedCommentDataTable GetFeedCommentEntity(int commentId)
       {
           NewsfeedDataSet ds = new NewsfeedDataSet();
           base.Helper.Fill(ds.T_FeedComment, "P_GetCommentEntity_ID", commentId);
           return ds.T_FeedComment;
       }

       public DateTime GetFeedPostLastestUpdateTime(int feedPostId)
       {
           return DateTime.Parse(base.Helper.ExecuteScalar("P_GetFeedPostLastestUpdateTime", feedPostId).ToString());
       }

       public NewsfeedDataSet.T_FeedCommentDataTable GetFeedCommentByNewsfeedId(int newsfeedId)
       {
           NewsfeedDataSet ds = new NewsfeedDataSet();
           base.Helper.Fill(ds.T_FeedComment, "P_GetCommentEntity_NewsfeedId", newsfeedId);
           return ds.T_FeedComment;
       }

       public NewsfeedDataSet.T_FeedCommentDataTable GetMoreCommentsByObj(int objId, int dataNum)
       {
           NewsfeedDataSet ds = new NewsfeedDataSet();
           base.Helper.Fill(ds.T_FeedComment, "P_GetComments_S_ObjId", objId, dataNum);
           return ds.T_FeedComment;
       }

       public NewsfeedDataSet.T_FeedCommentDataTable GetPostCommentByPostId(int feedpostId, int dataNum)
       {
           NewsfeedDataSet ds = new NewsfeedDataSet();
           base.Helper.Fill(ds.T_FeedComment, "P_GetAllComments_NewsfeedId", feedpostId, dataNum);
           return ds.T_FeedComment;
       }

       public bool DeletePostCommentById(int commentId)
       {
           try
           {
               Helper.ExecuteNonQuery("P_FeedComment_D_ID", commentId);
               return true;
           }
           catch (Exception ex)
           {
               return false;
           }
       }

       public void DeleteFeedPost(int feedpostId)
       {
           Helper.ExecuteNonQuery("P_DeleteFeedPost", feedpostId);
       }

       public DateTime GetLatestFeedPostUpdateTime(string appType, int appId)
       {
           NewsfeedDataSet ds = new NewsfeedDataSet();
           base.Helper.Fill(ds.T_FeedPost, "P_GetLatestFeedPost", appType, appId);
           if (ds.T_FeedPost.Rows.Count == 1)
           {
               return ds.T_FeedPost[0].CREATED_TIME;
           }
           else
           {
               return DateTime.Now;
           }
       }

       public NewsfeedDataSet.T_UserInfoDataTable GetUserInfoEntity(int userId)
       {
           NewsfeedDataSet ds = new NewsfeedDataSet();
           base.Helper.Fill(ds.T_UserInfo, "P_UserInfo_S_ID", userId);
           return ds.T_UserInfo;
       }
    }
}
